package main

import (
	"fmt"
)

type Car struct {
	Brand string
	Model string
}

type Cars []*Car

func (csp *Cars) ForEach(f func(*Car)) {
	for _, c := range *csp {
		f(c)
	}
}

func (csp *Cars) Filter(f func(cp *Car) bool) *Cars {
	result:=make(Cars,0)
	csp.ForEach(func(cp *Car) {
		if f(cp) {
			result=append(result,cp)
		}
	})
	return &result
}

func main() {
	cp1:=&Car{"BMW", "001"}
	cp2:=&Car{"Audi", "aaa"}
	cp3:=&Car{"BMW", "002"}
	cp4:=&Car{"Benz", "ooxx"}

	sp:=Cars{cp1,cp2,cp3,cp4}
	bmwSp:=sp.Filter(func(c *Car) bool{
		//return c.Brand==="BMW"
		return c.Brand=="BMW"
	})
	for _,v:=range *bmwSp {
		fmt.Println(v)
	}
}
